/*--------------------------------------------------------------------------
 *
 * 	You may freely copy, distribute, and reuse the code in this example.
 * 	SHL Systemhouse disclaims any warranty of any kind, expressed or  
 *	implied, as to its fitness for any particular use.
 *
 *
 *	Adaptor
 *
 *	Category Of:	EOFDelegate
 *
 *	Conforms To:	None
 *
 *	Declared In:	EOFDelegateAdaptorCategory.h
 *
 *
 *------------------------------------------------------------------------*/
#import "EOFDelegateAdaptorCategory.h"




@implementation EOFDelegate (Adaptor)

/*--------------------------------------------------------------------------
 *	EOAdaptor Delegate Methods
 *------------------------------------------------------------------------*/
- (BOOL)adaptor:(EOAdaptor *)adaptor willReportError:(NSString *)error
{
    // An adaptor sends this message to its delegate when an error has
    // occurred.  If the delegate returns NO, the adaptor doesn't open an
    // alert panel (or log the error to the console if it isn't in an
    // application).
	
	BOOL	result = YES;
	
	[[NXApp delegate] announce:adaptor 
		selector:_cmd 
		with:[NSArray arrayWithObject:error]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptor", 
			"%s", "YES", "NO", NULL, sel_getName(_cmd)))
			{
			case NX_ALERTALTERNATE:
				result = NO;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = YES;
				break;
			}
		}
	
	return result;
}


/*--------------------------------------------------------------------------
 * 	EOAdaptorContext Delegate Methods
 *
 * 	EOAdaptorContext sends messages to its delegate for any transaction
 * 	begin, commit, or rollback. The delegate can use these methods to
 * 	preempt these operations, modify their results, or simply track activity.
 *
 * 	EODelegateResponse is used to indicate the result a delegate demands
 * 	on notification that a particular action will be taken.  If a delegate
 * 	returns EODelegateRejects, the sender of the delegation message must
 * 	abort the current operation and return a failure result.  If the
 * 	delegate returns EODelegateApproves, the sender may continue the
 * 	operation and return whatever result is appropriate.  If the delegate
 * 	returns EODelegateOverrides, the sender must do nothing, but return a
 * 	success result.  When a delegate overrides an operation it's
 * 	responsible for seeing that the result of that operation is performed
 * 	successfully.
 *
 * 	typedef enum { 
 *     EODelegateRejects, EODelegateApproves, EODelegateOverrides
 * 	} EODelegateResponse;
 *
 *------------------------------------------------------------------------*/

- (EODelegateResponse)adaptorContextWillBegin:context
{
    // Invoked from -beginTransaction to tell the delegate that
    // a transaction is being started.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:context selector:_cmd];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorContext", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorContextDidBegin:context
{
    // Invoked from -beginTransaction or -transactionDidBegin to tell the
    // delegate that a transaction has begun. The delegate may take whatever
    // action it needs based on this information.

	[[NXApp delegate] announce:context selector:_cmd];
}


- (EODelegateResponse)adaptorContextWillCommit:context
{
    // Invoked from -commitTransaction to tell the delegate that
    // a transaction is being committed.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:context selector:_cmd];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorContext", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorContextDidCommit:context
{
    // Invoked from -commitTransaction or -transactionDidCommit to tell the
    // delegate that a transaction has been committed.  The delegate may take
    // whatever action it needs based on this information.

	[[NXApp delegate] announce:context selector:_cmd];
}


- (EODelegateResponse)adaptorContextWillRollback:context
{
    // Invoked from -rollbackTransaction to tell the delegate that
    // a transaction is being started.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:context selector:_cmd];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorContext", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorContextDidRollback:context
{
    // Invoked from -rollbackTransaction or -transactionDidRollback to tell
    // the delegate that a transaction has been rolled back.  The delegate may
    // take whatever action it needs based on this information.

	[[NXApp delegate] announce:context selector:_cmd];
}


/*--------------------------------------------------------------------------
 * 	EOAdaptorChannel Delegate Methods
 *
 *  EOAdaptorChannel sends messages to its delegate for nearly every
 *  operation that would affect data in the database server. The delegate can
 *  use these methods to preempt these operations, modify their results,
 *  or simply track activity.
 * 
 *  IMPORTANT: read the comments for the EODelegateResponse type in
 *  EOAdaptorContext.h for information on how the adaptor channel interprets
 *  delegate responses.
 *
 *------------------------------------------------------------------------*/

- (EODelegateResponse)adaptorChannel:channel
    willInsertRow:(NSMutableDictionary *)row
    forEntity:(EOEntity *)entity
{
    // Invoked from -insertRow:forEntity: to tell the delegate that a row is
    // being inserted. The delegate may modify row, changing the values for
    // particular keys (attributes), or even adding or deleting attributes.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects:row, entity, nil]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorChannel", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorChannel:channel
    didInsertRow:(NSDictionary *)row
    forEntity:(EOEntity *)entity
{
    // Invoked from -insertRow:forEntity: to tell the delegate that a row has
    // been inserted. The delegate may use this information to update
    // records, redisplay onscreen information, or take whatever other action
    // it needs.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects:row, entity, nil]];
}


- (EODelegateResponse)adaptorChannel:channel
    willUpdateRow:(NSMutableDictionary *)row
    describedByQualifier:(EOQualifier *)qualifier
{
    // Invoked from -updateRow:describedByQualifier: to tell the delegate
    // that a row is being updated.  The delegate can change the values for
    // particular attribute names in row, add or delete attributes, or modify
    // the qualifier.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects:row, qualifier, nil]];

	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorChannel", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorChannel:channel
    didUpdateRow:(NSDictionary *)row
    describedByQualifier:(EOQualifier *)qualifier
{
    // Invoked from -updateRow:describedByQualifier: to tell the delegate that
    // a row has been updated. The delegate may take whatever action it needs
    // based on this information.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects:row, qualifier, nil]];
}


- (EODelegateResponse)adaptorChannel:channel
    willDeleteRowsDescribedByQualifier:(EOQualifier *)qualifier
{
    // Invoked from -deleteRowsDescribedByQualifier: to tell the delegate
    // that a row is being deleted.  The delegate can modify the qualifier
    // to affect the result of the delete operation.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:qualifier]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorChannel", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorChannel:channel
    didDeleteRowsDescribedByQualifier:(EOQualifier *)qualifier
{
    // Invoked from -deleteRowsDescribedByQualifier: to tell the delegate that
    // some rows have been deleted. The delegate may take whatever action it
    // needs based on this information.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:qualifier]];
}


- (EODelegateResponse)adaptorChannel:channel
    willSelectAttributes:(NSMutableArray *)attributes
    describedByQualifier:(EOQualifier *)qualifier
    fetchOrder:(NSMutableArray *)fetchOrder
    lock:(BOOL)flag
{
    // Invoked from -selectAttributes:describedByQualifier:fetchOrder:lock:
    // to tell the delegate that a select operation is being performed.  The
    // delegate can modify attributes or the qualifier to affect the select
    // operation.
	
	int	result = EODelegateApproves;
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects: attributes, qualifier, fetchOrder, 
			[NSValue value:&flag withObjCType:"char"], nil]];
	
	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorChannel", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}
	

- (void)adaptorChannel:channel
    didSelectAttributes:(NSArray *)attributes
    describedByQualifier:(EOQualifier *)qualifier
    fetchOrder:(NSArray *)fetchOrder
    lock:(BOOL)flag
{
    // Invoked from -selectAttributes:describedByQualifier:fetchOrder:lock:
    // to tell the delegate that some rows have been selected. The delegate
    // may take whatever action it needs based on this information.
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObjects: attributes, qualifier, fetchOrder, 
			[NSValue value:&flag withObjCType:"char"], nil]];
}
	

- (NSMutableDictionary *)adaptorChannel:channel
    willFetchAttributes:(NSMutableArray *)attributes
    withZone:(NSZone *)zone
{
    // Invoked from -fetchAttributes:withZone: to tell the delegate that a
    // single row will be fetched. The delegate may modify attributes by
    // adding or deleting attributes. If the delegate returns a non-nil
    // value, that value will be treated as a dictionary resulting from a
    // fetch operation; the adaptor channel doesn't perform a fetch, and
    // -fetchAttributes:withZone: returns YES. If the delegate returns nil
    // the adaptor channel perfoms the fetch itself.
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:attributes]];

	return nil;
}


- (NSMutableDictionary *)adaptorChannel:channel
    didFetchAttributes:(NSDictionary *)attributes
    withZone:(NSZone *)zone
{
    // Invoked from -fetchAttributes:withZone: to tell the delegate that a
    // single row has been fetched.  The delegate may modify and return the
    // given dictionary of attributes, return a substitute dictionary, or nil
    // to cause -fetchAttributes:withZone: to fail and return NO.
	
	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:attributes]];

	return (NSMutableDictionary *)attributes;
}


- (void)adaptorChannelDidChangeResultSet:channel
{
    // Invoked from -fetchAttributes:withZone: to tell the delegate that
    // fetching will start for the next result set, when a select operation
    // resulted in multiple result sets.  This method is invoked just after a
    // -fetchAttributes:withZone: returns nil when there are still result sets
    // left to fetch.

	[[NXApp delegate] announce:channel selector:_cmd];
}


- (void)adaptorChannelDidFinishFetching:channel
{
    // Invoked from -fetchAttributes:withZone: to tell the delegate that
    // fetching is finished for the current select operation.  This method is
    // invoked when a fetch ends in -fetchAttributes:withZone: because there
    // are no more result sets.

	[[NXApp delegate] announce:channel selector:_cmd];
}


- (EODelegateResponse)adaptorChannel:channel 
    willEvaluateExpression:(NSMutableString *)expression
{
    // Invoked from -evaluateExpression: to tell the delegate that an
    // expression is about to be sent to the database server. The delegate
    // may modify expression to affect the result.

	int	result = EODelegateApproves;

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:expression]];

	if ([[NXApp delegate] wantsAlertPanels] == YES)
		{
		switch (NXRunAlertPanel ("EOAdaptorChannel", 
			"%s", "Approve", "Reject", "Override", sel_getName(_cmd)))
			{
			case NX_ALERTOTHER:
				result = EODelegateOverrides;
				break;
			case NX_ALERTALTERNATE:
				result = EODelegateRejects;
				break;
			case NX_ALERTDEFAULT:
			default:
				result = EODelegateApproves;
				break;
			}
		}
	
	return result;
}


- (void)adaptorChannel:channel
    didEvaluateExpression:(NSString *)expression
{
    // Invoked from -evaluateExpression: to tell the delegate that a query
    // language expression has been evaluated by the database server. The
    // delegate may take whatever action it needs based on this information.

	[[NXApp delegate] announce:channel 
		selector:_cmd 
		with:[NSArray arrayWithObject:expression]];
}


@end

